package com.wandousoushu.jiusen.ui.my

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.adapter.PayRuleAdapter
import com.wandousoushu.jiusen.base.BaseDialogFragment
import kotlinx.android.synthetic.main.dialog_vip_open.*
import org.jetbrains.anko.sdk27.listeners.onClick

class VipOpenDialog : BaseDialogFragment() {
     lateinit var adapter: PayRuleAdapter
     var ruleId =0

    override fun onStart() {
        super.onStart()
        val dm = DisplayMetrics()
        activity?.let {
            //Help.upSystemUiVisibility(it)
            it.windowManager?.defaultDisplay?.getMetrics(dm)
        }
        dialog?.window?.let {
            it.setBackgroundDrawableResource(R.color.transparent)
            it.decorView.setPadding(0, 0, 0, 0)
            val attr = it.attributes
            attr.dimAmount = 0.0f
            attr.gravity = Gravity.BOTTOM
            it.attributes = attr
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_vip_open, container)
    }

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        initData()
        initView()
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
    }

    private fun initData() {
        val bundle = arguments
        if (bundle != null) {
            val isrenew = bundle.getInt("isrenew")
            if (isrenew==1){
                vip_open.setText(R.string.vip_to_open_renew)
            }else{
                vip_open.setText(R.string.vip_to_open)
            }
        }
    }

    @SuppressLint("StringFormatInvalid", "StringFormatMatches")
    private fun initView() {
        //纵向线性布局
        val layoutManager = GridLayoutManager(requireContext(), 3)
        vip_recyclerview_rule.setLayoutManager(layoutManager)
        adapter = PayRuleAdapter(requireContext(), MyApplication.instance.getPayRuleList())
        adapter.setActionListener(PayRuleAdapter.ActionListener { payRule ->
            ruleId=payRule.id
            pay_info.setText(getString(R.string.vip_payrule_info_format, payRule.money,payRule.month))
        })
        vip_recyclerview_rule.setAdapter(adapter)
        adapter.SetSelect(0)

        vip_open.onClick {
            val intent = Intent(requireContext(), PayActivity::class.java)
            intent.putExtra("ruleId", ruleId)
            startActivity(intent)
            requireActivity().finish()
        }
    }

}
